package userInterface;

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.ACEmployee;
import model.Auction;
import model.Bidder;
import model.Control;
import model.NPOStaff;
import model.User;

/**
 * @author Igor
 */
@SuppressWarnings("serial")
public class LoginForm extends JFrame {

	private JPanel jContentPane;
	private JDesktopPane jDesktopPane;
	private JLabel lblUsername;
	private JTextField txtUsername;
	private JButton btnLogin;
	private JLabel lblMessage;

	private Control control;

	/**
	 * @author Major Bruno
	 * @author Daniel Bartholomew
	 */
	private void login() {
		User active;
		if (txtUsername.getText().trim().length() == 0) {
			lblMessage.setText("Username is required");
		} else {
			active = control.login(txtUsername.getText().trim());

			if (active instanceof ACEmployee) {
				new CalendarView(control);
				this.dispose();
			} else if (active instanceof Bidder) {
				new BidderMenuView(control);
				this.dispose();
			} else if (active instanceof NPOStaff) {
				Collection<Auction> auctions = control.getAuctions();
				boolean found = false;
				for (Auction a : auctions) {
					if (a.getNPO().equals(((NPOStaff) active).getNPO())) {
						found = true;
						new AuctionView(a, control);
						break;
					}
				}
				if (!found) {
					new AuctionForm(control);
				}
				this.dispose();
			} else {
				lblMessage.setText("User not found");
			}
		}

	}

	public LoginForm(final Control theControl) {
		control = theControl;
		this.setSize(640, 480);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
		initialize();
	}

	private void initialize() {
	
		jContentPane = new JPanel();

		jContentPane.setLayout(new BorderLayout());

		lblMessage = new JLabel();
		lblMessage.setBounds(new Rectangle(173, 91, 366, 21));

		lblUsername = new JLabel();
		lblUsername.setBounds(new Rectangle(176, 131, 86, 20));
		lblUsername.setText("Username: ");

		jDesktopPane = new JDesktopPane();

		jDesktopPane.add(lblUsername, null);

		txtUsername = new JTextField();
		txtUsername.setBounds(new Rectangle(265, 131, 169, 22));

		jDesktopPane.add(txtUsername, null);

		btnLogin = new JButton("Login");
		btnLogin.setBounds(new Rectangle(266, 200, 93, 26));
		btnLogin.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				login();
			}
		});
		jDesktopPane.add(btnLogin, null);
		jDesktopPane.add(lblMessage, null);
		jContentPane.add(jDesktopPane, BorderLayout.CENTER);
		this.setContentPane(jContentPane);
		this.setTitle("Login Form");
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	public JLabel getLblMessage() {
		return lblMessage;
	}

	public JTextField getTxtUsername() {
		return txtUsername;
	}

	public JButton getBtnLogin() {
		return btnLogin;
	}
}
